package api

import (
	"encoding/json"
	"fmt"
	"hft/tools/phone"
	"hft/tools/phone/model"
	"net/url"
	"strings"

	"github.com/tidwall/gjson"
)

type Audio struct{}

func (a *Audio) UploadAudio(audioList []*model.Audio) (code string) {

	jsonByte, _ := json.Marshal(audioList)

	jsonStr := string(jsonByte)

	data := phone.Phone400Api.BaseRequestV2("/addText.action", &url.Values{"data": []string{jsonStr}})
	fmt.Println(string(data))
	res := gjson.Get(string(data), "errcode").String()

	return res
}

// 删除语音
func (a *Audio) DeleteAudio(list []*model.AudioName) (result bool) {
	jsonByte, _ := json.Marshal(list)
	jsonStr := string(jsonByte)
	data := phone.Phone400Api.BaseRequestV2("/deleteVoice.action", &url.Values{"data": []string{jsonStr}})
	fmt.Println(string(data))
	errmsg := gjson.Get(string(data), "errmsg").String()
	return !strings.Contains(errmsg, "失败")
}

// 查询语音
func (a *Audio) FindAudio(filename string, pageSize int, pageNow int) []*model.AudioRemote {
	data := phone.Phone400Api.BaseRequestV2("/getVoice.action", &url.Values{"name": []string{filename}, "pageSize": []string{fmt.Sprintf("%v", pageSize)}, "pageNow": []string{fmt.Sprintf("%v", pageNow)}})
	rows := gjson.Get(string(data), "data.rows").String()
	fmt.Println(rows)
	var list []*model.AudioRemote
	json.Unmarshal([]byte(rows), &list)
	var nameList []*model.AudioName
	for _, v := range list {
		if v.AuditStatus != 1 {
			nameList = append(nameList, &model.AudioName{Name: v.Name})
		}
	}
	return list
}

// 查询语音
func (a *Audio) FindAllAudio(pageSize int, pageNow int) []*model.AudioRemote {
	data := phone.Phone400Api.BaseRequestV2("/getVoice.action", &url.Values{"pageSize": []string{fmt.Sprintf("%v", pageSize)}, "pageNow": []string{fmt.Sprintf("%v", pageNow)}})
	rows := gjson.Get(string(data), "data.rows").String()
	fmt.Println(rows)
	var list []*model.AudioRemote
	json.Unmarshal([]byte(rows), &list)
	var nameList []*model.AudioName
	for _, v := range list {
		if v.AuditStatus != 1 {
			nameList = append(nameList, &model.AudioName{Name: v.Name})
		}
	}
	return list
}

var PhoneAudio = new(Audio)
